package com.framework.modules.job.task;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.framework.entity.flow.FlowAccount;
import com.framework.entity.sys.SysAgent;
import com.framework.entity.sys.SysDept;
import com.framework.entity.sys.SysLvRate;
import com.framework.entity.ter.TerMerchantReport;
import com.framework.entity.ter.TerTerminalInfo;
import com.framework.entity.tran.TranBenefitDay;
import com.framework.entity.tran.TranTerBenefitDay;
import com.framework.entity.tran.TranTerBenefitInfo;
import com.framework.entity.tran.TranTransactionInfo;
import com.framework.service.*;
import com.framework.utils.DateUtils;
import com.framework.utils.string.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 根据报备机具来计算分润 考拉超收
 *
 * @author wq
 */
@Transactional(rollbackFor = Exception.class)
@Component("tranBenefitForKaola")
public class TranBenefitForKaola {


    @Autowired
    private TransactionInfoService transactionInfoService;

    @Autowired
    private BenefitDayService benefitDayService;

    @Autowired
    private FlowAccountService flowAccountService;
    @Autowired
    private DeptService deptService;

    @Autowired
    private TerFlowInfoService terFlowInfoService;

    private Logger logger = LoggerFactory.getLogger(getClass());


    private Date benefitDate;
    private String benefitDateStr;
    private Date today;

    public void countBenefit() {
        //初始化时间
        initialDate();
        //计算分润
        countPersonalBenefit();
        //添加流水信息
        addFlowAccount();
        //添加信息
        addSnInfo();
    }

    private void initialDate() {
        //获取当前日期
        today = DateUtils.stringToDate(DateUtils.format(new Date()), DateUtils.DATE_PATTERN);
        benefitDate = DateUtils.addDateDays(today, -1);
        benefitDateStr = DateUtils.format(benefitDate, DateUtils.DATE_PATTERN);
    }

    private void addSnInfo() {
        List<TranTransactionInfo> list = transactionInfoService.selectList(
                new EntityWrapper<TranTransactionInfo>()
                        .eq("transaction_date", benefitDateStr)
                        .eq("is_benefited", 0)
                        .eq("dept_id", 889326L)
        );
        for (TranTransactionInfo info : list) {
            terFlowInfoService.addVerifyTranInfo(info.getSn(), info.getDeptId());
        }

    }

    /**
     * 计算每个报备机器的交易分润，具体到机器
     */
    private void countPersonalBenefit() {

        logger.info("开始计算分润");

        SysDept sysDept = deptService.queryDeptById(889326L);

        transactionInfoService.benefitByMerchant(sysDept,benefitDateStr);

        logger.info("分润计算完毕");
    }


    /**
     * 导入流水
     */
    public void addFlowAccount() {
        //获取当前日期
        logger.info("流水记录导入开始");
        //查询今天的分润汇总
        List<TranBenefitDay> list = benefitDayService.queryToAddFlowAccount(benefitDateStr);
        List<TranBenefitDay> tranBenefitDays = list.stream().filter(e -> e.getDeptId().equals(889326L)).collect(Collectors.toList());

        for (TranBenefitDay tranBenefitDay : tranBenefitDays) {
            flowAccountService.addFlowAccountAfterBenefit(tranBenefitDay);
        }
        logger.info("流水记录导入结束");
    }

}
